<?php
class Tool {
	/**
	* @var mixed Mysql connection
	*/
	var $mysqli = null;
	/**
	* @var array User credentials (id, email, type)
	*/
	var $user = null;
	/**
	* @var array Notifications
	*/
	var $notif = array();
	/**
	* @var string Area of concentration
	*/
	var $area = null;
	
	/**
	* Tool constructor
	* Initializes tool variables for intended request
	* 
	* @param string $area Area of concentration
	* @param resource $mysqli Current mysql connection
	* @param resource $user User credentials (id, email, type)
	*/
	function __construct($area, $mysqli, $user) {
		$this->mysqli = $mysqli;
		$this->user = $user;
		$this->area = $area;
	}

	/**
	* Generate ID
	* Generates a new ID with hash value on area provided,
	* date of generation and unique id
	* 
	* @param string $for Area of generated ID
	*
	* @return string ID
	*/
	public static function generateID($for) {
		return substr(hash('adler32', $for), 4).'-'.uniqid();
	}

	/**
	* Truncate string
	* Removes excess from the string provided, truncated it with an ellipsis
	* 
	* @param string $string String to be truncated
	* @param integer $limit Maximum characters of string
	*
	* @return string Truncated string
	*/
	public static function truncate($string, $limit) {
		# Check if string is more than limit
		# If greater, truncate. Else, leave as is
		return((empty($string) or strtolower(self::trimout($string))=='null') ? 'Not available' : ((strlen($string) > $limit) ? substr($string, 0, $limit).'&#133;' : $string));
	}

	/**
	* Trimout string
	* Removes unnecessary characters from a string
	* 
	* @param string $string String to be trimmed out
	*
	* @return string Trimmed out string
	*/
	public static function trimout($string) {
		# Remove excess spaces, add slashes for special characters
		return trim(addslashes(preg_replace('/\s+/', ' ', $string)));
	}
	
	/**
	* Escape string
	* Prepare string for mysql query
	* 
	* @param string $string String to be prepared
	*
	* @return string Escaped string
	*/
	public static function mysqlEscStr($string) {
		return mysql_real_escape_string($string);
	}

	/**
	* Process URL variables
	* Process URL variables, such as query string, filter options, etc.
	* 
	* @param array $opts Variables to process
	*
	* @return string URL variables
	*/
	public static function optstrify($opts) {
		# Empty variable array
		if(!is_array($opts)) return '';
		$optstr = '';
		
		# Process each variable
		foreach($opts as $k => $o) {
			if(in_array($k, array('s', 'view', 'fail', 't', 'optn', 'area', 'q'))) continue;

			$optstr .= (isset($o) and $o!='') ? "&{$k}={$o}" : '';
		}
		
		# Return resulting string
		return substr($optstr, 1);
	}
	
	/**
	* Delete all files
	* Delete all files from the system
	* 
	* @param string $dir Directory
	* @param array $inc Directory included in deletion
	*
	* @return boolean True on success, false on failure
	*/
	public static function deleteAllFiles($dir, $inc) {
		# Open directory
		if($dh = opendir($dir)) {
			$files = array();
			$inner_files = array();
			
			# Iterate through each file
			while($file = readdir($dh)) {
				if($file != "." && $file != ".." && $file[0] != '.') {
					$dirfile = $dir."/".$file;
					
					# File existing
					if(file_exists($dirfile)) {
						# Directory
						if(is_dir($dirfile)) {
							# Recursively delete files in folder
							$inner_files = self::deleteAllFiles($dirfile, false);
							if(is_array($inner_files)) $files = array_merge($files, $inner_files);
							rmdir($dirfile);
						}
						# File
						else {
							array_push($files, $dirfile);
							unlink($dirfile);
						}
					}
				}
			}
			
			# Close directory
			closedir($dh);
			
			if($inc===true) {
				rmdir($dir);
			}
			
			return true;
		}
		
		return false;
	}
}
?>